package com.mmy.controller.restaurant;

import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mmy.domain.constzone.TConstZone;
import com.mmy.domain.restaurant.VRestaurantBase;
import com.mmy.domain.restaurant.VRestaurantPlan;
import com.mmy.domain.sys.TSysOrg;
import com.mmy.service.impl.constzone.ConstZoneServiceImpl;
import com.mmy.service.impl.restaurant.RestaurantBaseServiceImpl;
import com.mmy.service.impl.restaurant.RestaurantPlanServiceImpl;
import com.mmy.service.impl.sys.SysOrgServiceImpl;

@Controller
public class RestaurantBaseController {
	@Autowired
	RestaurantBaseServiceImpl restaurantBaseServiceImpl;
	@Autowired
	RestaurantPlanServiceImpl restaurantPlanServiceImpl;
	@Autowired
	ConstZoneServiceImpl constZoneServiceImpl;
	@Autowired
	SysOrgServiceImpl sysOrgServiceImpl;

	/**
	 * 函数功能说明： 跳转--餐馆首页
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 liys 修改人 hanxl
	 * @date 创建日期2021.03.01 修改时间2021.03.17
	 */
	@RequestMapping("/tofrontrestindex")
	public String toFrontRestIndex(HttpServletRequest request, Model model, HttpSession session, String pageNumber,
			String pageSize) {
		// 获取当前定位
		String strCityName = (String) session.getAttribute("sessionCity");
		if (strCityName == null) {
			strCityName = "上海市";
		}
		TConstZone dbConstZone = new TConstZone();
		// 搜索条件
		String strRestName = request.getParameter("strRestName");
		String strProvinceID = request.getParameter("strProvinceID");
		String strCityID = request.getParameter("strCityID");
		String strRestPrice1 = request.getParameter("strScenicPrice1");
		String strRestPrice2 = request.getParameter("strScenicPrice2");
		if (strCityID == null || strCityID == "") {
			// 查询当前定位的ZoneID
			dbConstZone = constZoneServiceImpl.getZoneID(strCityName);
		} else {
			dbConstZone = constZoneServiceImpl.findByZoneID(strCityID);
		}

		// 查询该市的所有景区

		// 查询计划日期大于当前日期的景区ID 价格区间在搜索范围的
		List<Integer> lstRestID = restaurantPlanServiceImpl.getRestID(strRestPrice1, strRestPrice2);
		List<VRestaurantBase> lstRestaurantBase = restaurantPlanServiceImpl.findBySearch(pageNumber, pageSize,
				lstRestID, dbConstZone.getZoneID(), strRestName);

		for (VRestaurantBase vRestaurantBase : lstRestaurantBase) {
			Integer iZoneID = vRestaurantBase.getZoneID();
			String province = constZoneServiceImpl.findProvinceStr(iZoneID);
			String city = constZoneServiceImpl.findCityStr(iZoneID);
			vRestaurantBase.setZoneName(province + city + vRestaurantBase.getZoneName());
		}
		model.addAttribute("lstRestaurantBase", lstRestaurantBase);
		// 当前时间有效的计划最小价格
		List<VRestaurantPlan> lstRestaurantPlan = restaurantPlanServiceImpl.getMinPrice();
		model.addAttribute("lstRestaurantPlan", lstRestaurantPlan);
		/* 查询所有区域 国家或者省市区 */
		List<TConstZone> lstConstZoneProvince = constZoneServiceImpl.findAllProvince();
		model.addAttribute("lstConstZoneProvince", lstConstZoneProvince);
		HashMap<String, Integer> mapPage = restaurantBaseServiceImpl.mapFrontRestBase(pageNumber, pageSize, lstRestID);

		model.addAttribute("iPageNo", mapPage.get("iPageNo"));
		model.addAttribute("iTotalCount", mapPage.get("iTotalCount"));
		model.addAttribute("iMaxPage", mapPage.get("iMaxPage"));
		model.addAttribute("iPageSize", mapPage.get("iPageSize"));
		if (!(strProvinceID == "" || strProvinceID == null)) {
			// 根据省ID查出该省所有的市
			List<TConstZone> lstConstZoneCity = constZoneServiceImpl.findCities(Integer.parseInt(strProvinceID));
			model.addAttribute("lstConstZoneCity", lstConstZoneCity);
		}

		model.addAttribute("strRestName", strRestName);
		model.addAttribute("strProvinceID", strProvinceID);
		model.addAttribute("strCityID", strCityID);
		model.addAttribute("strRestPrice1", strRestPrice1);
		model.addAttribute("strRestPrice2", strRestPrice2);
		return "rest/RestIndex";

	}

	/**
	 * 函数功能说明： 跳转--餐馆详情页
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 liys
	 * @date 创建日期2021.03.01
	 */
	@RequestMapping(value = "/tofrontrestinfo")
	public String ToFrontRestaurantBaseInfo(HttpServletRequest request, Model model) {

		VRestaurantBase dbRestaurantBase = restaurantBaseServiceImpl
				.findByRestaurantID(request.getParameter("iRestaurantID"));
		model.addAttribute("dbRestaurantBase", dbRestaurantBase);
		List<VRestaurantPlan> lstRestaurantPlan = restaurantPlanServiceImpl
				.getByRestaurantID(request.getParameter("iRestaurantID"));
		model.addAttribute("lstRestaurantPlan", lstRestaurantPlan);
		// 通过所属区域查询省和市
		String strProvince = constZoneServiceImpl.findProvince(dbRestaurantBase.getZoneID()).getZoneName();
		String strCity = constZoneServiceImpl.findCity(dbRestaurantBase.getZoneID()).getZoneName();
		model.addAttribute("strProvince", strProvince);
		model.addAttribute("strCity", strCity);
		// 查询景点所属公司名称
		TSysOrg dbSysOrg = sysOrgServiceImpl.findByOrgID(Integer.parseInt(dbRestaurantBase.getSpare1()));
		model.addAttribute("strOrgName", dbSysOrg.getOrgName());
		return "rest/RestaurantInfo";
	}

	/**
	 * 函数功能说明：跳转预留页
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 liys
	 * @date 创建日期
	 */
	@RequestMapping(value = "/restreserve")
	public String EnterReserveRestaurant(HttpServletRequest request, Model model) {

		int iRestaurantID = Integer.parseInt(request.getParameter("iRestID"));
		// List<VRestaurantPlan> lstRestaurantPlan =
		// vRestaurantPlanServiceImpl.findByRestaurantID(iRestaurantID);
		// model.addAttribute("restaurantplanlist", lstRestaurantPlan);
		model.addAttribute("iRestaurantID", iRestaurantID);
		return "rest/reserverestaurant";
	}
	
	/**
	  * @descr 级联选择器,本地餐馆
	  * @param 
	  * @return 
	  * @throws 
	  * @author liys
	  * @date 2021-6-21
	 */
	@RequestMapping("/getRestsByZone")
	@ResponseBody
	public String getScenicsByOrg(HttpServletRequest request,HttpSession session) {
		
		//TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
		//int iOrgID = tSysUser.getOrgID();
		int iZoneID = Integer.parseInt(request.getParameter("iZoneID"));
		String res = restaurantBaseServiceImpl.restSelect(iZoneID);
		
		return res;
	}
}
